package restless.core {
	
	import flash.net.registerClassAlias;
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	import flash.utils.IExternalizable;
	
	/**
	 * 
	 * @author Jordan Laine 05/25/12
	 *
	 */
	 
	 public class RLEStat implements IExternalizable {
		 
		registerClassAlias("Boolean", Boolean);
		registerClassAlias("Number", Number);
		registerClassAlias("String", String);
		
		public var id:String;
		public var gameID:String;
		public var location:int;
		public var complete:Boolean = false;
		public var achievement:Boolean = false;
		
		public var peakCount:Number = 0;
		public var currentCount:Number = 0;
		public var sessionCount:Number = 0;
		public var historyCount:Number = 0;
		
		private var _resetable:Boolean;
		private var _negative:Boolean;
		
		public function init(gameID:String, statID:String, loc:int, resetable:Boolean = false, negative:Boolean = false):void {
			id = statID;
			gameID = gameID;
			location = loc;
			_resetable = resetable;
			_negative = negative;
		}
		
		public function compareHistoryCount(num:int):Boolean {
			return (num >= historyCount)?true:false;
		}
		
		public function compareGameHigh(num:int):Boolean {
			return (num >= peakCount)?true:false;
		}
		
		public function compareCurrentCount(num:int):Boolean {
			return (num >= currentCount)?true:false;
		}
		
		public function add(num:Number):void {
			var tempNum:Number = num;
			currentCount += tempNum;
			
			if (tempNum > 0) {
				historyCount += tempNum;
			}
			
			if (currentCount < 0) {
				if (!_negative) {
					currentCount = 0;
				} 
			}
			
			if (currentCount > peakCount) {
				peakCount = currentCount;
			}
		}
		
		public function resetCurrent():void {
			currentCount = 0;
		}
		
		public function resetSession():void {
			sessionCount = 0;
		}
		
		public function ressetGameHigh():void {
			if (_resetable) {
				peakCount = 0;
			}
		}
		
		public function resetHistoryCount():void {
			if (_resetable) {
				historyCount = 0;
			}
		}
		
		public function zero():void {
			if (_resetable) {
				peakCount = 0;
				currentCount = 0;
				historyCount = 0;
				sessionCount = 0;
				complete = false;
			}
		}
		
		public function set negative(bool:Boolean):void {
			_negative = bool;
		}
		
		public function get negative():Boolean {
			return _negative;
		}
		
		public function set resetable(bool:Boolean):void {
			_resetable = bool;
		}
		
		public function get resetable():Boolean {
			return _resetable;
		}
		
		public function traceData():void {
			trace(id + " Creation Values: currentCount = " + currentCount + " | sessionCount = " + sessionCount + " | historyCount = " + historyCount + " | gameHigh = " + peakCount); 
		}
		
		public function writeExternal(output:IDataOutput):void {
			output.writeInt(currentCount);
		}
		
		public function readExternal(input:IDataInput):void {
			currentCount = input.readInt();
		}
	}
}